import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/authStore'
import RegisterView from '../views/RegisterView.vue'
import DashboardView from '../views/DashboardView.vue'
import ForgetAccountsView from '@/views/ForgetAccountsView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'root',
      component: { template: '<router-view />' },
      beforeEnter: (to, from, next) => {
        const authStore = useAuthStore()
        if (authStore.isAuthenticated) {
          next({ name: 'dashboard' })
        } else {
          next({ name: 'login-web' })
        }
      },
    },
    {
      path: '/login/',
      name: 'login-web',
      component: () => import('@/views/LoginView.vue'),
    },
    { path: '/register', name: 'register', component: RegisterView },
    { path: '/find-account', name: 'find-account', component: ForgetAccountsView },

    { path: '/register', name: 'register', component: RegisterView },
    { path: '/find-account', name: 'find-account', component: ForgetAccountsView },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: DashboardView,
      meta: { requiresAuth: true },
    },

    {
      path: '/reset-password',
      name: 'reset-password',
      component: () => import('@/views/EmailAndAccountsView.vue'),
    },

    // 学生路由
    {
      path: '/practice',
      name: 'practice',
      component: () => import('../views/StudentView/PracticeView.vue'),
      meta: { requiresAuth: true, roles: ['STUDENT'] },
    },
    {
      path: '/user/profile',
      component: () => import('../views/UserProfileView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/error-notebook',
      name: 'error-notebook',
      component: () => import('../views/StudentView/ErrorNotebookView.vue'),
      meta: { requiresAuth: true, roles: ['STUDENT'] },
    },
    {
      path: '/my-classroom',
      name: '/my-classroom',
      component: () => import('../views/StudentView/MyClassView.vue'),
      meta: { requiresAuth: true, roles: ['STUDENT'] },
    },
    {
      path: '/ability-analysis',
      name: 'ability-analysis',
      component: () => import('../views/StudentView/AbilityAnalysisView.vue'),
      meta: { requiresAuth: true, roles: ['STUDENT'] },
    },
    // { path: '/assignment/:id', name: 'assignment-detail', component: () => import('../views/AssignmentView.vue'), meta: { requiresAuth: true, roles: ['STUDENT'] } },

    // 教师路由
    {
      path: '/teacher/dashboard',
      name: 'teacherDashboard',
      component: () => import('../views/TeacherView/TeacherDashboard.vue'),
      meta: { requiresAuth: true, roles: ['TEACHER'] },
    },
    {
      path: '/teacher/classes',
      name: 'teacherClasses',
      component: () => import('../views/TeacherView/ClassManagementView.vue'),
      meta: { requiresAuth: true, roles: ['TEACHER'] },
    },
    {
      path: '/teacher/upload-question',
      name: 'teacherUploadQuestion',
      component: () => import('../views/TeacherView/TeacherCreateQuestionView.vue'),
      meta: { requiresAuth: true, roles: ['TEACHER'] },
    },
    {
      path: '/teacher/classes/:classId/assignments',
      name: 'AssignmentList',
      component: () => import('@/views/TeacherView/TeacherAssignmentClassView.vue'),
      meta: { requiresAuth: true, roles: ['TEACHER'] },
    },
    {
      path: '/assignments/:assignmentId/submissions',
      name: 'AssignmentSubmissions',
      component: () => import('@/views/TeacherView/AssignmentSubmissionsView.vue'),
    },
    {
      path: '/admin/users',
      name: 'adminUserManagement',
      component: () => import('../views/AdminView/AdminUserManagement.vue'),
      meta: { requiresAuth: true, roles: ['ADMIN'] },
    },
    {
      path: '/admin/upload-question',
      name: 'upload-question',
      component: () => import('../views/AdminView/AdminQuestionUploaderView.vue'),
      meta: { requiresAuth: true, roles: ['ADMIN'] },
    },
    {
      path: '/admin/upload-knowledge-points',
      name: 'upload-knowledge-points',
      component: () => import('../views/AdminView/KnowledgePointCreateView.vue'),
      meta: { requiresAuth: true, roles: ['ADMIN'] },
    },
    {
      path: '/admin/pending-question',
      name: 'pending-question',
      component: () => import('../views/AdminView/AdminQuestionPendingview.vue'),
      meta: { requiresAuth: true, roles: ['ADMIN'] },
    },
    {
      path: '/admin/approved-question',
      name: 'approved-question',
      component: () => import('../views/AdminView/AdminApprovedQuestionsView.vue'),
      meta: { requiresAuth: true, roles: ['ADMIN'] },
    },
    {
      path: '/questions/:id',
      name: 'QuestionDetail',
      component: () => import('@/views/AdminView/QuestionDetailsView.vue'),
    },
    {
      path: '/assignment/:id',
      name: 'assignment-detail',
      component: () => import('../views/StudentView/AssignmentView.vue'),
      meta: { requiresAuth: true, roles: ['STUDENT'] },
    },
  ],
})

router.beforeEach(async (to, from) => {
  const authStore = useAuthStore()

  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    authStore.returnUrl = to.fullPath
    return { name: 'login' }
  }

  const requiredRoles = to.meta.roles as string[] | undefined
  if (requiredRoles && authStore.user && !requiredRoles.includes(authStore.user.role)) {
    return { name: 'dashboard' }
  }
})

export default router
